﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace WindowsFormsApplication3
{
    public partial class QuotationConfirmationForm : Form
    {
          private String connString = Program.ConnStr;
        public QuotationConfirmationForm()
        {
            InitializeComponent();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }





        public void combojobcodeboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();
            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select jobpk,jobcode from  jobcodemastertable", oleDbConnection1);
            OleDbDataReader reader = oleDbCommand1.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Columns.Add("jobpk", typeof(int));
            dt.Columns.Add("jobcode", typeof(string));
            dt.Load(reader);
            cmbjobcode.ValueMember = "jobpk";
            cmbjobcode.DisplayMember = "jobcode";
            cmbjobcode.DataSource = dt.DefaultView;

            oleDbConnection1.Close();

        }

        private void QuotationConfirmationForm_Load(object sender, EventArgs e)
        {
            combojobcodeboxload();
            
        }


        public void comboQuotationboxload()
        {

            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();

            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from  quotationmastertable  where jobpk = " + cmbjobcode.SelectedValue + " AND isconfirmed ='F' ", oleDbConnection1);

         
           
            OleDbDataReader reader = oleDbCommand1.ExecuteReader();
     
           
            DataTable dt = new DataTable();
            
            dt.Columns.Add("quotationpk", typeof(int));
            dt.Columns.Add("quotationcode", typeof(string));
            dt.Columns.Add("amount", typeof(int));
            dt.Load(reader);
            if (dt.Rows.Count != 0)
            {
            cmbQuotationcode.ValueMember = "quotationpk";
            cmbQuotationcode.DisplayMember = "quotationcode";
            cmbQuotationcode.DataSource = dt.DefaultView;
            txtamount.Text = dt.Rows[0]["amount"].ToString();



            oleDbConnection1.Close();
            }
            else{
                MessageBox.Show("NO QUOTATION OF SAME");
                clearcontrol();
            }
          
        }

        private void cmbjobcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboQuotationboxload();
            txtconfirmamount.Text = "";
        }

        private void cmbQuotationcode_SelectedIndexChanged(object sender, EventArgs e)
        {
            OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
            oleDbConnection1.Open();

            OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("Select quotationpk ,quotationcode , amount from  quotationmastertable  where quotationpk = " + cmbQuotationcode.SelectedValue + " AND isconfirmed ='F' ", oleDbConnection1);



            OleDbDataReader reader = oleDbCommand1.ExecuteReader();

            DataTable dt = new DataTable();
           
            
                dt.Columns.Add("quotationpk", typeof(int));
                dt.Columns.Add("quotationcode", typeof(string));
                dt.Columns.Add("amount", typeof(int));
                dt.Load(reader);
                 if (dt.Rows.Count != 0)
                 {
                //  cmbQuotationcode.ValueMember = "quotationpk";
                //  cmbQuotationcode.DisplayMember = "quotationcode";
                //  cmbQuotationcode.DataSource = dt.DefaultView;
                txtamount.Text = dt.Rows[0]["amount"].ToString();
                oleDbConnection1.Close();
            }
            else
            {
                MessageBox.Show("NO QUOTATION ");
                clearcontrol();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtconfirmamount.Text.Trim() == null || txtconfirmamount.Text.Trim() == "")
            {

                txtconfirmamount.Focus();
                lblstatus.Text = "Enter an valid Amount";
                txtconfirmamount.Text = "";
            }
            else
            {
                try
                {
                    Int32 confirmedamount = Int32.Parse(txtconfirmamount.Text);
                }
                catch (Exception)
                {
                    lblstatus.Text = "EnterThe Amount";
                    txtconfirmamount.Text = "";

                }


               
            
            
            }


          try
           {

                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();
                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("INSERT INTO confirmquotationmastertable (jobcode ,jobpk , quotationcode , quotationpk , amount , agrdamount) Values ( @jobcode , @jobpk ,@quotationcode , @quotationpk , @amount , @agrdamount) ", oleDbConnection1);
                oleDbCommand1.Parameters.AddWithValue("@jobcode", cmbjobcode.Text);
                oleDbCommand1.Parameters.AddWithValue("@jobpk", cmbjobcode.SelectedValue);
                oleDbCommand1.Parameters.AddWithValue("@quotationcode", cmbQuotationcode.Text);
                oleDbCommand1.Parameters.AddWithValue("@quotationpk", cmbjobcode.SelectedValue);
                oleDbCommand1.Parameters.AddWithValue("@amount", int.Parse(txtamount.Text));
                oleDbCommand1.Parameters.AddWithValue("@agrdamount", int.Parse(txtconfirmamount.Text));



                oleDbCommand1.CommandType = CommandType.Text;


                oleDbCommand1.ExecuteNonQuery();

                oleDbConnection1.Close();
                clearcontrol();
               
                MessageBox.Show("Confirmed Quotation" +cmbQuotationcode.Text+"");
                removeconfirmedquotation();
                this.Close();

          }
            catch (Exception exp)
           {
              MessageBox.Show("The application had met with some errors please restart  the application :\n error:" + exp + "");

            }

          

        }

        public void removeconfirmedquotation()
            
        {
           try
           {
              

                OleDbConnection oleDbConnection1 = new System.Data.OleDb.OleDbConnection(connString);
                oleDbConnection1.Open();
                OleDbCommand oleDbCommand1 = new System.Data.OleDb.OleDbCommand("UPDATE quotationmastertable SET isconfirmed = 'T' WHERE quotationpk = " + cmbQuotationcode.SelectedValue + " ", oleDbConnection1);
                


                oleDbCommand1.CommandType = CommandType.Text;


                oleDbCommand1.ExecuteNonQuery();

                oleDbConnection1.Close();
                clearcontrol();
               

           }
           catch (Exception exp)
           {
                MessageBox.Show("The application had met with some errors  AT removeconfirmedquotation please restart  the application :\n error:" + exp + "");

           }



        }


        public void clearcontrol()
        {
           
            txtamount.Text = "";
            txtconfirmamount.Text = "";
        }
















    }
}
